package com.ktjy.listener;

import com.ktjy.pojo.SysUser;
import com.ktjy.service.ISysUserService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TaskListenerImpl implements TaskListener {
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public void notify(DelegateTask delegateTask) {
        String name = delegateTask.getName();
        String postName = name.substring(0, name.length() - 2);
//        SysUser sysUser = sysUserService.findByPostName(postName);
//        if (sysUser != null) {
//            delegateTask.setAssignee(sysUser.getRealName());
//        }
        List<SysUser> users = sysUserService.findByPostName(postName);
        if (users != null && !users.isEmpty()) {
            // 添加候选用户组
            for (SysUser user : users) {
                delegateTask.addCandidateUser(user.getRealName());
            }
        }
    }
}
